﻿using Shop.Model.ActivityGoods;
using Shop.Model.DB;
using Shop.RemoteModel;
using Shop.RemoteModel.ActivityGoods.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Shop.Repository.lmpl
{
    internal class ActivityGoodsRepository : ShopBaseRepository<DBShopActivityGoods, long>, IActivityGoodsRepository
    {
        public ActivityGoodsRepository (ISqlClientFactory factory) : base(factory)
        {
        }
        public GoodsPitchInActivity[] GetPitchInActivity (DateTime begin, DateTime end, long[] skuId)
        {
            ActivityType[] types = new ActivityType[]{
                ActivityType.套装促销,
                ActivityType.单品促销
            };
            return base.Join<DBShopActivity, GoodsPitchInActivity>((a, c) => c.Id == a.ActivityId &&
            skuId.Contains(a.SkuId) &&
            types.Contains(c.ActivityType) &&
            c.BeginTime <= end &&
            c.EndTime >= begin && c.ActivityStatus == ActivityStatus.已审核, (a, c) => new GoodsPitchInActivity
            {
                ActivityId = a.ActivityId,
                ActivityPrice = a.ActivityPrice,
                ActivityType = c.ActivityType,
                ActivityTitle = c.ActivityTitle,
                SkuId = a.SkuId,
            });
        }
        public void Sync (DBShopActivityGoods[] adds, long[] dropId, long activityId, int num)
        {
            ISqlQueue<DBShopActivityGoods> queue = base.BeginQueue();
            queue.UpdateOneColumn<DBShopActivity>(c => c.SkuNum == num, c => c.Id == activityId);
            if (!adds.IsNull())
            {
                queue.Insert(adds);
            }
            if (!dropId.IsNull())
            {
                queue.Delete(a => dropId.Contains(a.Id));
            }
            _ = queue.Submit();
        }
        public Result[] GetsByActivityId<Result> (long activityId) where Result : class, new()
        {
            return base.Gets<Result>(a => a.ActivityId == activityId);
        }

        public DBShopActivityGoods[] Adds (long activityId, ActivityGoods[] goods)
        {
            DBShopActivityGoods[] adds = goods.ConvertAll(c => new DBShopActivityGoods
            {
                ActivityId = activityId,
                GoodsId = c.GoodsId,
                SkuId = c.SkuId,
                Id = IdentityHelper.CreateId()
            });
            long len = goods.Length;
            ISqlQueue<DBShopActivityGoods> queue = base.BeginQueue();
            queue.UpdateOneColumn<DBShopActivity>(c => c.SkuNum == len, c => c.Id == activityId);
            queue.Insert(adds);
            queue.Submit();
            return adds;
        }
        public void SetPrice (Dictionary<long, decimal> price)
        {
            if (price.Count == 1)
            {
                KeyValuePair<long, decimal> key = price.First();
                this._SetPrice(key.Key, key.Value);
                return;
            }
            ISqlQueue<DBShopActivityGoods> queue = base.BeginQueue();
            price.ForEach((id, val) =>
            {
                queue.UpdateOneColumn(a => a.ActivityPrice == val, a => a.Id == id);
            });
            queue.Submit();
        }
        private void _SetPrice (long id, decimal price)
        {
            if (!base.Update(a => a.ActivityPrice == price, a => a.Id == id))
            {
                throw new ErrorException("shop.activity.price.set.fail");
            }
        }
        public void SetPrice (long id, decimal price, bool isMust)
        {
            if (!base.Update(new DBShopActivityGoods
            {
                ActivityPrice = price,
                IsMust = isMust,
                Id = id,
            }, new string[]
            {
                "ActivityPrice",
                "IsMust"
            }))
            {
                throw new ErrorException("shop.activity.price.set.fail");
            }
        }
    }
}
